Imports DTO
Imports System.Data.OleDb

Public Class RangBuocLopHocDao
    Inherits AbstractDao
    Public Sub New()

    End Sub
    Public Function LayBang() As DataTable
        Dim dt As New DataTable
        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = Me.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Select * From RangBuocLopHoc"
        'B4: Thuc thi chuoi strSQL
        Dim da As OleDbDataAdapter
        da = New OleDbDataAdapter(strSQL, cn)
        da.Fill(dt)
        'B5: Dong ket noi CSDL
        cn.Close()
        Return dt
    End Function
    Public Sub CapNhatBang(ByVal dt As DataTable)
        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = Me.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Select * From RangBuocLopHoc"
        'B4: Thuc thi chuoi strSQL
        Dim da As OleDbDataAdapter
        da = New OleDbDataAdapter(strSQL, cn)
        Dim cb As New OleDbCommandBuilder(da)
        da.Update(dt)
        'B5: Dong ket noi CSDL
        cn.Close()
    End Sub

    Public Function LayDanhSach() As IList
        Dim ds As New ArrayList()
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        Dim cn As OleDbConnection
        cn = Me.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Select * From RangBuocLopHoc"
        Dim cmd As New OleDbCommand(strSQL, cn)
        Dim dr As OleDbDataReader
        dr = cmd.ExecuteReader()
        While (dr.Read())
            Dim rblhDto As New RangBuocLopHocDto
            rblhDto.MaRangBuocLopHoc = dr("MaRangBuocLopHoc")
            rblhDto.MaLopHoc = dr("MaLopHoc")
            rblhDto.Thu = dr("Thu")
            rblhDto.TietHoc = dr("TietHoc")
            rblhDto.TrangThai = dr("TrangThai")

            ds.Add(rblhDto)

        End While
        'B5: Dong ket noi CSDL
        dr.Close()
        cn.Close()
        Return ds
    End Function
    Public Function TimKiemtheoma(ByVal maRBLH As String)
        Dim rblhDto As New RangBuocLopHocDto
        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = Me.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Select * From RangBuocLopHoc Where MaRangBuocLopHoc = " & maRBLH
        'B4: Thuc thi chuoi strSQL
        Dim cmd As New OleDbCommand(strSQL, cn)
        Dim dr As OleDbDataReader
        dr = cmd.ExecuteReader()
        While (dr.Read())
            rblhDto.MaRangBuocLopHoc = dr("MaRangBuocLopHoc")
            rblhDto.MaLopHoc = dr("MaLopHoc")
            rblhDto.Thu = dr("Thu")
            rblhDto.TietHoc = dr("TietHoc")
            rblhDto.TrangThai = dr("TrangThai")

        End While
        'B5: Dong ket noi CSDL
        dr.Close()
        cn.Close()
        Return rblhDto
    End Function
    Public Sub Them(ByVal rblhDto As RangBuocLopHocDto)
        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = Me.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Insert into RangBuocLopHoc( MaLopHoc,Thu,TietHoc,TrangThai)) values " & _
                 " ( '" & rblhDto.MaLopHoc & "' ," & rblhDto.Thu & " ," & rblhDto.TietHoc & ", " & _
                 rblhDto.TrangThai & ")"

        'B4: Thuc thi chuoi strSQL
        Dim cmd As New OleDbCommand(strSQL, cn)
        cmd.ExecuteNonQuery()

        strSQL = "Select @@IDENTITY"
        cmd = New OleDbCommand(strSQL, cn)
        rblhDto.MaRangBuocLopHoc = cmd.ExecuteScalar()

        'B5: Dong ket noi CSDL
        cn.Close()
    End Sub
    Public Sub Xoa(ByVal maRBLH As String)
        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = Me.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Delete From RangBuocLopHoc " & _
                 " Where MaRangBuocLopHoc = " & maRBLH
        'B4: Thuc thi chuoi strSQL
        Dim cmd As New OleDbCommand(strSQL, cn)
        cmd.ExecuteNonQuery()
        'B5: Dong ket noi CSDL
        cn.Close()
    End Sub
    Public Sub Sua(ByVal rblhDto As RangBuocLopHocDto)
        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = Me.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Update RangBuocLopHoc Set " & _
                 " MaLopHoc = '" + rblhDto.MaLopHoc & "'and Thu = " + rblhDto.Thu + " and TietHoc = " & rblhDto.TietHoc & _
                 " and TrangThai = " & rblhDto.TrangThai & _
                 " Where MaRangBuocLopHoc= " & rblhDto.MaRangBuocLopHoc
        'B4: Thuc thi chuoi strSQL
        Dim cmd As New OleDbCommand(strSQL, cn)
        cmd.ExecuteNonQuery()
        'B5: Dong ket noi CSDL
        cn.Close()
    End Sub



End Class
